<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Retrieve extends CI_Controller {
 
	public function index(){
		$plancode = $this->uri->segment(3,0);
		$userprofile = $this->getUserProfile($plancode);
		if($userprofile)
		{
			// get image from SHOP API
			$sku = $userprofile->gadgetSKU;
			$userprofile->imgURL = $this->getImageFromShop($sku);

			// get nickname from db
			$userFromDB = $this->getDetailsFromDB("0" . $userprofile->msisdn);
			if($userFromDB) {
				$userprofile->nName = $userFromDB['nName'];
				$userprofile->otherCont = $userFromDB['otherCont']; 				
			} else {
				$userprofile->nName = "";
				$userprofile->otherCont = "";
			}
			
			// get info from api
			$d['data']=$userprofile; 
			$views['_content'] = "retrieve";
			$this->template->load('template_landing', $views, $d);
		} else {
			redirect(base_url('index.php/welcome'));
		}
	}

	private function getDetailsFromDB($mobtel){
		$this->load->model('transaction');	 
		$userProfile = $this->transaction->retrieveUserProfileToDB($mobtel);
		
		//print_r($userProfile[0]);
		
		if($userData = $userProfile[0])
		{
			return $userData;
		}
		return false;
	}
	
	private function getImageFromShop($sku){
		$this->config->load('config_maitai_shopapi');
		$ch = curl_init($this->config->item('maitai_shopapi_url') . $sku);    // initialize curl handle
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$product_array = curl_exec($ch);  
		$product = json_decode($product_array);
		//print_r($product); die();
		try {
			return $product[0]->imageurl;
		} catch (Exception $e) {
			return "";			
		}
	}
	
	private function getUserProfile($plancode=''){
		if($plancode!='') {
			// check with hessian api
			$this->load->library('Maitaidata');
			$subsdetails = $this->maitaidata->getSubscriptionDetails($plancode);
			$subsresp = $subsdetails->obj;

			if($subsresp!=NULL)
			{
				return $subsresp;
			} 
			return false;
		}
		return false;
	}
	
	/*
	 * SAMPLE RETURN NG DATA
	 * 
	 * 
	 *     ["planCode"]=>
    string(6) "100LBP"
    ["planStatus"]=>
    string(1) "1"
    ["fName"]=>
    string(4) "TEST"
    ["mName"]=>
    string(4) "TEST"
    ["lName"]=>
    string(4) "TEST"
    ["email"]=>
    string(18) "ian@fluxion.com.ph"
    ["msisdn"]=>
    string(10) "9159252640"
    ["birthdate"]=>
    string(10) "09/16/2014"
    ["streetNum"]=>
    string(5) "qqweq"
    ["area"]=>
    string(17) "Port Area (South)"
    ["town"]=>
    string(6) "Manila"
    ["province"]=>
    string(12) "Metro Manila"
    ["postalCode"]=>
    string(4) "1018"
    ["gadgetId"]=>
    string(2) "13"
    ["gadgetName"]=>
    string(10) "Ace 4 Lite"
    ["gadgetBrand"]=>
    string(7) "Samsung"
    ["gadgetPlanPrice"]=>
    string(6) "5000.0"
    ["gadgetPrice"]=>
    string(6) "4540.9"
    ["gadgetSKU"]=>
    string(18) "samsung-ace-4-lite"
    ["imgURL"]=>
    NULL
    ["promoId"]=>
    string(2) "22"
    ["promoName"]=>
    string(10) "ALLNET1300"
    ["promoTypeId"]=>
    string(2) "13"
    ["promoTypeName"]=>
    string(12) "ALLNET TEXTS"
    ["promoTypeDesc"]=>
    string(45) "unliallnet texts, Globe/TM calls, mobile data"
    ["validity"]=>
    string(2) "30"
    ["validityDesc"]=>
    string(7) "30 days"
    ["promoDesc"]=>
    string(76) "360 mins of calls to Globe/TM, 20 mins of allnet calls and unli allnet texts"
    ["promoCallDesc"]=>
    string(54) "360 mins of calls to Globe/TM, 20 mins of allnet calls"
    ["promoTextDesc"]=>
    string(17) "unli allnet texts"
    ["promoSurfDesc"]=>
    NULL
    ["promoPrice"]=>
    string(6) "1300.0"
    ["commitmentPeriod"]=>
    string(1) "6"
    ["totalRebate"]=>
    string(6) "1296.0"
    ["seededRebate"]=>
    string(3) "0.0"
    ["rebateAmt"]=>
    string(5) "216.0"
    ["storeId"]=>
    string(1) "1"
    ["storeName"]=>
    string(15) "UP Techno Store"
    ["storeAddress"]=>
    string(57) "UP Ayalaland TechnoHub Diliman, Commonwealth, Quezon City"
    ["storeHours"]=>
    string(9) "9AM - 6PM"
    ["storeArea"]=>
    string(3) "GMA"
    ["longitude"]=>
    string(9) "321.22212"
    ["latitude"]=>
    string(9) "323.12312"
	 * 
	 * 
	 */
	
	
}